package com.yonyou.iuap.jxzy.ref.service;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import com.yonyou.iuap.jxzy.ref.dao.RefMapper;
import com.yonyou.iuap.mybatis.type.PageResult;

@Service
public class RefService {
	

	public RefMapper refMapper;
	
	@Autowired
	public void setRefMapper(RefMapper refMapper) {
		this.refMapper = refMapper;
	}

	public Page<Map<String, Object>> getTreeRefData(PageRequest page,String tableName,String[] strFieldCode,
			String refCode, String refName, String refID,String wheresql){
		if(tableName == null || tableName.trim().length() == 0){
			return null;
		}
		String sql = getSql(tableName, strFieldCode, refCode, refName, refID, wheresql);
 		
		PageResult<Map<String, Object>> pageResult = this.refMapper.selectSQL(page, sql);
		return pageResult.getPage();
	}
	
	/**
	 * 构造SQL语句
	 * 
	 * @author wxf
	 */
	protected String getSql(String tableName,String[] strFieldCode,
			String refCode, String refName, String refID,String wheresql) {
//		StringBuffer whereClause = new StringBuffer();
		StringBuffer sql = new StringBuffer("select ");
		if(!StringUtils.isEmpty(refCode)){
			sql.append(refCode + " refCode, " );
		}
		if(!StringUtils.isEmpty(refName)){
			sql.append(refName + " refName, " );
		}
		if(!StringUtils.isEmpty(refID)){
			sql.append(refID + " refID, " );
		}
		if(strFieldCode != null || strFieldCode.length > 0){
			for (int i = 0; i < strFieldCode.length; i++) {
				sql.append(strFieldCode[i]);
				if (i < strFieldCode.length - 1)
					sql.append(",");
			}
		}
		sql.append(" from ("+tableName+") where 11=11 ");
		//加入where条件
		if(!StringUtils.isEmpty(wheresql)){
			sql.append(wheresql);
		}
		return sql.toString();
	}
	
	
	/**
	 * 构造基本 SQL
	 */
//	public String buildBaseSql(String patch, String[] columns,
//			String[] hiddenColumns, String tableName, String whereCondition) {
//		StringBuffer whereClause = new StringBuffer();
//		StringBuffer sql = new StringBuffer("select ").append(patch)
//				.append(" ");
//		int columnCount = columns == null ? 0 : columns.length;
//		addQueryColumn(columnCount, sql, columns, hiddenColumns);
//		// 加入FROM子句
//		sql.append(" from ").append(tableName);
//		// 加入WHERE子句
//		if (whereCondition != null && whereCondition.trim().length() != 0) {
//			whereClause.append(" where (").append(whereCondition).append(" )");
//		} else
//			whereClause.append(" where 11=11 ");
//
////		appendAddWherePartCondition(whereClause);
//
////		addDataPowerCondition(getTableName(), whereClause);
////		addDisabledDataWherePart(whereClause);
////		addEnvWherePart(whereClause);
//		sql.append(" ").append(whereClause.toString());
//
//		return sql.toString();
//	}
	
//	/**
//	 * 添加列条件
//	 * 
//	 * @param iSelectFieldCount
//	 * @param strSql
//	 * @param strFieldCode
//	 * @param hiddenFields
//	 */
//	public void addQueryColumn(int iSelectFieldCount, StringBuffer strSql,
//			String[] strFieldCode, String[] hiddenFields) {
//
////		int nameFieldIndex = getFieldIndex(/*getRefNameField()*/);
//
//		for (int i = 0; i < iSelectFieldCount; i++) {
//			strSql.append(strFieldCode[i]);
//			if (i < iSelectFieldCount - 1)
//				strSql.append(",");
//		}
//		// 加入隐藏字段
//		if (hiddenFields != null && hiddenFields.length > 0) {
//			for (int k = 0; k < hiddenFields.length; k++) {
//				if (hiddenFields[k] != null
//						&& hiddenFields[k].trim().length() > 0) {
//					strSql.append(",");
//					strSql.append(hiddenFields[k]);
//				}
//			}
//		}
//	}
	
}

